//
//  YBNewsListViewController.m
//  GoodBuy
//
//  Created by Cyber on 15/5/13.
//  Copyright (c) 2015年 GoodBuy. All rights reserved.
//

#import "YBNewsListViewController.h"

@interface YBNewsListViewController ()

@end

@implementation YBNewsListViewController

-(void)dealloc {
    [_newsListTableView release];
    [super dealloc];
}
- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"资讯";
    // Do any additional setup after loading the view.
    self.newsListTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    self.newsListTableView.delegate = self;
    self.newsListTableView.dataSource = self;
    [self.view addSubview:_newsListTableView];
    [_newsListTableView release];
    
//    _newsListTableView.separatorStyle = UITableViewScrollPositionNone;
    
    //菊花旋转
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:_HUD];
    _HUD.labelText = @"亲 请稍等";
    _HUD.tintColor = [UIColor cyanColor];
    [_HUD showAnimated:YES whileExecutingBlock:^{
        //刷新时间最大上限
        //        [self showtime];
    } completionBlock:^{
        
    }];
    
    self.allNewsArray = [[NSMutableArray alloc]init];

    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    
    NSString *url_string = [NSString stringWithFormat:@"http://api.smzdm.com/v1/news/articles?limit=20"];
    
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        //        NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        NSDictionary *dic1 = [[NSDictionary alloc]init];
        dic1 = responseObject;
        NSMutableDictionary *dic2 = [[NSMutableDictionary alloc]init];
        dic2 = [dic1 objectForKey:@"data"];
        NSMutableArray *arr = [[NSMutableArray alloc]init];
        arr = [dic2 objectForKey:@"rows"];
        
        for (NSDictionary *dic in arr) {
            YBNewsListModel *newsList = [[YBNewsListModel alloc]init];
            [newsList setValuesForKeysWithDictionary:dic];
            [_allNewsArray addObject:newsList];
            [newsList release];
        }
        [_newsListTableView reloadData];
        [_HUD removeFromSuperview];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        [_HUD removeFromSuperview];

    }];
    //下拉刷新
    [_newsListTableView addHeaderWithCallback:^{
        AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
        
        
        NSString *url_string = [NSString stringWithFormat:@"http://api.smzdm.com/v1/news/articles?limit=20"];
        
        
        //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
        
        AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
        [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            [netWorkManager stopMonitoring];
            //        NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
            NSDictionary *dic1 = [[NSDictionary alloc]init];
            dic1 = responseObject;
            NSMutableDictionary *dic2 = [[NSMutableDictionary alloc]init];
            dic2 = [dic1 objectForKey:@"data"];
            NSMutableArray *arr = [[NSMutableArray alloc]init];
            arr = [dic2 objectForKey:@"rows"];
            [_allNewsArray removeAllObjects];
            for (NSDictionary *dic in arr) {
                YBNewsListModel *newsList = [[YBNewsListModel alloc]init];
                [newsList setValuesForKeysWithDictionary:dic];
                [_allNewsArray addObject:newsList];
                [newsList release];
            }
            [_newsListTableView reloadData];
            [_newsListTableView headerEndRefreshing];

        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
            NSLog(@"失败==== %@",error);
            [_newsListTableView headerEndRefreshing];

        }];
    }];
    //上拉加载
    [_newsListTableView addFooterWithCallback:^{
        AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
        YBNewsListModel *listModel = [[YBNewsListModel alloc]init];
        listModel = [_allNewsArray lastObject];
        
        NSString *url_string = [NSString stringWithFormat:@"http://api.smzdm.com/v1/news/articles?limit=20&article_date=%@", listModel.article_date];
        NSString *url = [url_string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
        
        AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
        [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            [netWorkManager stopMonitoring];
            //        NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
            NSDictionary *dic1 = [[NSDictionary alloc]init];
            dic1 = responseObject;
            NSMutableDictionary *dic2 = [[NSMutableDictionary alloc]init];
            dic2 = [dic1 objectForKey:@"data"];
            NSMutableArray *arr = [[NSMutableArray alloc]init];
            arr = [dic2 objectForKey:@"rows"];
            
            for (NSDictionary *dic in arr) {
                YBNewsListModel *newsList = [[YBNewsListModel alloc]init];
                [newsList setValuesForKeysWithDictionary:dic];
                [_allNewsArray addObject:newsList];
                [newsList release];
            }
            [_newsListTableView reloadData];
            [_newsListTableView footerEndRefreshing];
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
            NSLog(@"失败==== %@",error);
            [_newsListTableView footerEndRefreshing];
            
        }];
        
    }];
    
}
//设置section个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

//设置每个section的row个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_allNewsArray count];
}

//设置每个cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIndentifier = @"cell";
    YBNewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil) {
        cell = [[YBNewsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    cell.newsListModel = [_allNewsArray objectAtIndex:indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    YBNewsDetailViewController *newsDetailVC = [[YBNewsDetailViewController alloc]init];
    newsDetailVC.newsListModel = [_allNewsArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:newsDetailVC animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [UIScreen mainScreen].bounds.size.height/4 + 20;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
